<template>
  <page-header-wrapper :title="false">
    <a-card :body-style="{padding: '24px 32px'}" :bordered="false">
      <a-row :gutter="16">
        <a-col :md="24" :lg="12">
          <a-form>
            <a-form-item label="用户名(必填)" :required="true">
              <a-input placeholder="请输入用户名" v-model="formData.username"/>
            </a-form-item>
            <a-form-item label="密码" :required="true">
              <a-input placeholder="请输入账户密码" v-model="formData.password"/>
            </a-form-item>
            <a-form-item label="归口部门">
              <a-input placeholder="请输入归口部门" v-model="formData.department"/>
            </a-form-item>
          </a-form>
          <a-button type="primary" @click="handleSubmit()" class="add-btn">保存</a-button>
        </a-col>
      </a-row>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { createUser } from '@/api/login'
export default {
  data () {
    return {
      formData: {
        username: '',
        password: '',
        department: ''
      }
    }
  },
  methods: {
    handleSubmit () {
      let msg = ''
      if (!this.formData.username) {
        msg = '用户名必须填写'
      }
      if (msg) {
        this.$notification['error']({
          message: '错误',
          description: msg,
          duration: 4
        })
        return
      }
      this.submit()
    },
    submit () {
      return new Promise((resolve, reject) => {
        createUser(this.formData).then(response => {
          this.setSuccess()
          resolve()
        }).catch(error => {
          this.requestFailed(error)
          reject(error)
        })
      })
    },
    setSuccess () {
      this.$notification['success']({
        message: '成功',
        description: '用户添加成功',
        duration: 4
      })
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '用户添加失败',
        description: err,
        duration: 4
      })
    }
  }
}
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }
  .ant-form-item {
    margin-bottom: 0;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .add-btn {
    margin-top: 10px;
  }
</style>
